package io.bruce.akka.actor.master;

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.bruce.akka.common.MathCalculate;

import java.util.HashMap;
import java.util.Map;

public class MasterCalculateTest {

    public static void main(String[] args) {
        Map<String, Object> propMap = new HashMap<>();
        propMap.put("akka.actor.provider", "akka.remote.RemoteActorRefProvider");
        propMap.put("akka.remote.netty.tcp.hostname", "127.0.0.1");
        propMap.put("akka.remote.netty.tcp.port", 8989);

        Config config = ConfigFactory.parseMap(propMap);
        ActorSystem system = ActorSystem.create("master-server", config);
        ActorRef masterActor = system.actorOf(Props.create(MasterResultActor.class), "master-actor");

        ActorSelection actorSelection = system.actorSelection("akka.tcp://math-worker@127.0.0.1:8899/user/worker-actor");
        for(int i =0; i < 5; i++) {
            final int num = i;
            new Thread(() -> actorSelection.tell(new MathCalculate(num, num, MathCalculate.OperateEnum.ADD), masterActor)).start();
        }
    }
}
